package com.night.icm.controller.core;

import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.model.core.ChangePasswordInfoDTO;
import com.night.icm.model.core.UserDTO;
import com.night.icm.service.core.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * UserController
 *
 * @author Liao ZhiYong
 * @date 2019/12/3
 */
@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/api/icm-user")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation(value = "新建用户")
    @PostMapping(value = "")
    public UserDTO createUser(@ApiParam(value = "用户信息", required = true) @RequestBody UserDTO userDto) {
        return userService.createUser(userDto);
    }

    @ApiOperation(value = "分页查询用户信息")
    @GetMapping(value = "")
    public PageDataDTO<UserDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页纪录条数", required = true, defaultValue = "20") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "用户名，支持模糊查询") @RequestParam(value = "username", required = false) String username,
            @ApiParam(value = "角色名称") @RequestParam(value = "role", required = false) String role
    ) {
        return this.userService.findOnePage(page, size, sort, username, role);
    }

    @ApiOperation(value = "根据id查找用户")
    @GetMapping(value = "/{id}")
    public UserDTO findById(@ApiParam(value = "用户id", required = true) @PathVariable(name = "id") String id) {
        return this.userService.findById(id);
    }

    @ApiOperation(value = "根据用户名查找用户")
    @GetMapping(value = "/findByName")
    public UserDTO findByName(@ApiParam(value = "用户名", required = true) @RequestParam(value = "name") String name) {
        return this.userService.findByName(name);
    }

    @ApiOperation(value = "查找所有用户")
    @GetMapping(value = "/all")
    public List<UserDTO> findAll() {
        return this.userService.findAll();
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping(value = "/{id}")
    public void delete(@ApiParam(value = "用户Id", required = true) @PathVariable(name = "id") String id) {
        this.userService.deleteById(id);
    }

    @ApiOperation(value = "更新用户信息")
    @PutMapping(value = "/{id}")
    public UserDTO update(
            @ApiParam(value = "用户id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "用户信息", required = true) @RequestBody @Validated UserDTO userDTO) {
        return this.userService.update(id, userDTO);
    }

    @ApiOperation(value = "修改用户密码")
    @PutMapping(value = "/updateUserPassword/{id}")
    public Boolean updateUserPassword(
            @ApiParam(value = "用户id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "用户修改密码信息", required = true) @RequestBody @Validated ChangePasswordInfoDTO changePasswordInfoDTO
    ) {
        return this.userService.updateUserPassword(id, changePasswordInfoDTO.getOldPassword(), changePasswordInfoDTO.getNewPassword());
    }
//
//    /**
//     * 用户注册
//     *
//     * @param user          用户信息
//     * @return 操作结果
//     * @throws AuthenticationException 错误信息
//     */
//    @PostMapping(value = "/register")
//    public String register(@RequestBody User user) throws AuthenticationException {
//        return userService.register(user);
//    }
//
//    /**
//     * 刷新密钥
//     *
//     * @param authorization 原密钥
//     * @return 新密钥
//     * @throws AuthenticationException 错误信息
//     */
//    @GetMapping(value = "/refreshToken")
//    public String refreshToken(@RequestHeader String authorization) throws AuthenticationException {
//        return userService.refreshToken(authorization);
//    }
}